/*
 * Created by JFormDesigner on Fri Dec 25 20:33:08 CST 2020
 */

package cn.fourhundredandeighteen.view.AdminView.AdminInfo;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;

import cn.fourhundredandeighteen.UserInfo;
import cn.fourhundredandeighteen.entity.Admin;
import cn.fourhundredandeighteen.service.AdminService;
import cn.fourhundredandeighteen.service.impl.AdminServiceImpl;
import cn.fourhundredandeighteen.utill.NumberUtil;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.Component.Container;

/**
 * @author Br.S
 */
public class AdminInfo extends JPanel {
    public AdminInfo() {
        initComponents();
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
    }

    private void updataMouseClicked(MouseEvent e) {
        //单击事件
        //获取文本中数据
        String name = textField2.getText();
        String password = textField3.getText();
        if (!NumberUtil.Password(password)) {
            JOptionPane.showConfirmDialog(null,"密码格式错误", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        }else{
            //更新数据
            admin.setAdminName(name);
            admin.setAdminPassword(password);
            admin.setUpdateTime(new Date());
            int i = adminService.upAdminInfo(admin);
            if (i > 0) {
                JOptionPane.showConfirmDialog(null, "修改成功", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
                Container.getInstance().upDatePage();
            } else {
                JOptionPane.showConfirmDialog(null, "修改失败", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
            }
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        label4 = new JLabel();
        textField4 = new JTextField();
        label5 = new JLabel();
        textField5 = new JTextField();
//        updata = new Button();
        updata = new Button();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {87, 69, 0, 0, 207, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {43, 55, 55, 51, 55, 55, 29, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

        //---- label1 ----
        label1.setText("账号");
        label1.setMaximumSize(new Dimension(23, 25));
        add(label1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField1, new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label2 ----
        label2.setText("名称");
        add(label2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField2, new GridBagConstraints(2, 2, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label3 ----
        label3.setText("密码");
        add(label3, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField3, new GridBagConstraints(2, 3, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label4 ----
        label4.setText("创建时间");
        add(label4, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField4, new GridBagConstraints(2, 4, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label5 ----
        label5.setText("修改时间");
        add(label5, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField5, new GridBagConstraints(2, 5, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- 数据更新 ----
        //获取数据
        if (admin==null){
            JOptionPane.showConfirmDialog(null,"管理员不存在", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        }
        textField1.setText(String.valueOf(admin.getAdminId()));
        textField1.setEnabled(false);
        textField2.setText(admin.getAdminName());
        textField3.setText(admin.getAdminPassword());
        textField4.setText(admin.getCreateTime().toGMTString());
        textField4.setEnabled(false);
        textField5.setText(admin.getUpdateTime().toString());
        textField5.setEnabled(false);

        //---- updata ----
        updata.setText("\u4fee\u6539");
        updata.addActionListener(e -> button1ActionPerformed(e));
        updata.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                updataMouseClicked(e);
            }
        });
        add(updata, new GridBagConstraints(2, 6, 2, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 15), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JLabel label4;
    private JTextField textField4;
    private JLabel label5;
    private JTextField textField5;
    private JButton updata;
    private AdminService adminService=new AdminServiceImpl();
    private Admin admin = adminService.getAdmin(UserInfo.Session);
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
